home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 May: Tool Chest / Developer CD Series Tool Chest (Apple Computer)(May 1999).iso / Tool Chest / Devices / CD-ROM / iso9660 / Srcs / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-09-28  |  7.8 KB  |  374 lines  |  [TEXT/MPS ]

  1. /************************************************************************
  2.  *
  3.  *  Module:            main.c
  4.  *
  5.  *  Purpose:        Macintosh flow control
  6.  *
  7.  *  Description:    This module contains all the Macintosh flow control
  8.  *                    for the ISO9660 program.
  9.  *
  10.  *    Copyright © 1990 Apple Computer, Inc.  All rights reserved.
  11.  *
  12.  ************************************************************************/
  13. #include <stdio.h>
  14. #include <QuickDraw.h>
  15. #include <Menus.h>
  16. #include <Memory.h>
  17. #include <Events.h>
  18. #include <Windows.h>
  19. #include <Dialogs.h>
  20. #include <Desk.h>
  21. #include <Files.h>
  22. #include <ToolUtils.h>
  23. #include <DiskInit.h>
  24. #include <Fonts.h>
  25.  
  26. #include "HighSierra.h"
  27. #include "BuildISO.h"
  28. #include "DialogUtils.h"
  29. #include "BuildISO.proto.h"
  30. #include "ErrorMsg.proto.h"
  31. #include "i_o.proto.h"
  32.  
  33. QDGlobals    qd;
  34.  
  35. Str255            drvName = "\p.Sony";        /* name of the device driver for isoOpen() */
  36.  
  37. enum {
  38.     AppleID = 128,
  39.     FileID,
  40.     EditID
  41. };
  42.  
  43. enum {
  44.     AppleM = 1,
  45.     FileM,
  46.     EditM
  47. };
  48.  
  49. #define    PROMPTID    258
  50.  
  51. #define    HELP        1
  52. #define CREATEFILES    2
  53. #define    QUIT        4
  54.  
  55. #define MENUCOUNT    3
  56.  
  57. #include "main.proto.h"
  58. #include "MyDialog.proto.h"
  59.  
  60. static void DoDisk(long message);
  61. static void InitializeVolume(void);
  62.  
  63. MenuHandle    gMenus[MENUCOUNT];
  64. Boolean        gQuit;
  65. EventRecord    gEvent;
  66. WindowPtr    gWindow;
  67.  
  68. short    gRefNum;
  69. short    gDriveNumber = 0;
  70.  
  71. static void DoDisk(long);
  72. static void    InitializeVolume(void);
  73.  
  74. void SetUpMenus(void);
  75. void DoAbout(void);
  76. void DoCommand(long);
  77. void HandleEvent(EventRecord *);
  78. void main(void);
  79.  
  80.  
  81. /************************************************************************
  82.  *
  83.  *  Function:        SetUpMenus
  84.  *
  85.  *  Purpose:        initialize menus
  86.  *
  87.  *  Returns:        nothing
  88.  *
  89.  *  Side Effects:    standard apple menus are set up
  90.  *
  91.  *  Description:    loop through MENUCOUNT times, inserting the menu
  92.  *                    we got using GetMenu.  Draw the menu bar.
  93.  *
  94.  ************************************************************************/
  95. void
  96. SetUpMenus(void)
  97. {
  98.     short    i;
  99.     
  100.     gMenus[AppleM] = GetMenu(AppleID);
  101.     AddResMenu(gMenus[AppleM], 'DRVR');    /* add desk accessories */
  102.     gMenus[FileM] = GetMenu(FileID);
  103.     gMenus[EditM] = GetMenu(EditID);        /* only used by desk access. */
  104.     
  105.     for (i = 1; i <= MENUCOUNT; i++)
  106.         InsertMenu(gMenus[i], 0);    /*install at end of Menu Bar */
  107.  
  108.     /* disable creating files until we initialize a floppy */
  109. /***TO DO***    DisableItem(gMenus[FileM], CREATEFILES); */
  110.     DrawMenuBar();
  111. }
  112.  
  113.  
  114. /************************************************************************
  115.  *
  116.  *  Function:        DoAbout
  117.  *
  118.  *  Purpose:        give ownership & copyright information
  119.  *
  120.  *  Returns:        nothing
  121.  *
  122.  *  Side Effects:    nothing
  123.  *
  124.  *  Description:    put up an alert, telling about us.
  125.  *
  126.  ************************************************************************/
  127. void
  128. DoAbout(void)
  129. {
  130.     Alert(DU_CenterALRT(128), 0L);
  131. }
  132.  
  133.  
  134.  
  135. /************************************************************************
  136.  *
  137.  *  Function:        DoCommand
  138.  *
  139.  *  Purpose:        Handle command or command key equivalent
  140.  *
  141.  *  Returns:        void
  142.  *
  143.  *  Side Effects:    whatever it executes may have a side effect.
  144.  *
  145.  *  Description:    Look at the menu selected and the item of that menu,
  146.  *                    and do a case statement on the item.  Execute the
  147.  *                    appropriate item.
  148.  *
  149.  ************************************************************************/
  150. void
  151. DoCommand(long mResult)
  152. {
  153.     short    theItem;
  154.     short    theMenu;
  155.     Str255 name;
  156.     GrafPtr savePort;
  157.     
  158.     theItem = mResult &0xFFFF;
  159.     theMenu = (mResult >> 16) & 0xFFFF;
  160.     
  161.     switch (theMenu) {
  162.     case AppleID:
  163.         if (theItem == 1)     /* tell about the program */
  164.             DoAbout();
  165.         else {
  166.             GetPort(&savePort);
  167.             GetItem(gMenus[AppleM], theItem, name);
  168.             OpenDeskAcc(name);
  169.             SetPort(savePort);
  170.         }
  171.         break;
  172.     case FileID:
  173.         switch (theItem)
  174.         {
  175.             case    HELP:
  176.                 Help();
  177.                 break;
  178.             case    CREATEFILES:
  179.                 CreatePathTable(gRefNum);
  180.                 CreateFiles(gRefNum);
  181.                 /* DisableItem(gMenus[FileM], CREATEFILES); */
  182.                 break;
  183.             case    QUIT:
  184.                 gQuit = true;
  185.                 break;
  186.         }
  187.     case EditID:
  188.         SystemEdit(theItem - 1);
  189.         break;
  190.     }
  191.     HiliteMenu(0);
  192. }
  193.  
  194.  
  195.  
  196. /************************************************************************
  197.  *
  198.  *  Function:        DoDisk
  199.  *
  200.  *  Purpose:        Handle disk inserted events
  201.  *
  202.  *  Returns:        nothing
  203.  *
  204.  *  Side Effects:    may initialize a disk
  205.  *
  206.  *  Description:    check the return code from the mount attempt (the
  207.  *                    high word of the event message coming in.)  If the
  208.  *                    mount failed, go ahead and initialize it using DIFormat.
  209.  *                    This gets the basic format information, but no file
  210.  *                    system information.
  211.  *
  212.  ************************************************************************/
  213.  static void
  214.  DoDisk(long message)
  215.  {
  216.      OSErr    result;
  217.      CursHandle    cursor;
  218.      
  219.      gDriveNumber = message & 0xFFFF;
  220.      result = (message >> 16) & 0xFFFF;
  221.      
  222.      /* if a mount failed (e.g. blank floppy) then we can format the floppy
  223.      ** without doing anything.  If the mount succeeded, the floppy was in
  224.      ** some format that the Mac recognized.  Unmount the volume and flush
  225.      ** it out of the mac without ejecting it, and *then* format it.
  226.      */
  227.      if (result == noErr)
  228.      {
  229.          if (AskDestroyDisk(gDriveNumber) == true)
  230.              result = UnmountVol(NULL, gDriveNumber);
  231.          else
  232.          {
  233.              UnmountVol(NULL, gDriveNumber);
  234.              Eject(NULL, gDriveNumber);
  235.              return;                /* get rid of that volume and return now! */
  236.          }
  237.      }
  238.      
  239.      cursor = GetCursor(watchCursor);
  240.      if (!cursor)
  241.          SetCursor(*cursor);
  242.      result = DIFormat(gDriveNumber);
  243.     InitializeVolume();
  244.     EnableItem(gMenus[FileM], CREATEFILES);
  245.     SetCursor(&qd.arrow);
  246.          
  247.      if (result != noErr)
  248.          ErrorMsg("Can't format volume! (result = %d)", result);
  249. }
  250.  
  251.  
  252.  
  253. void
  254. HandleEvent(EventRecord *gEvent)
  255. {
  256.     DialogPtr    dPtr;
  257.     short        itemHit;
  258.     
  259.     switch (gEvent->what) {
  260.     case mouseDown:
  261.         switch (FindWindow(gEvent->where, &gWindow)) {
  262.         case inSysWindow:
  263.             SystemClick(gEvent, gWindow);
  264.             break;
  265.         case inMenuBar:
  266.             DoCommand(MenuSelect(gEvent->where));
  267.             break;
  268.         }    /* end MouseDown */
  269.         break;
  270.     case keyDown:
  271.     case autoKey:
  272.         if ((gEvent->modifiers & cmdKey) != 0)
  273.             DoCommand (MenuKey ((char) (gEvent->message & 0xFF)));
  274.         break;
  275.     case diskEvt:
  276.         DoDisk(gEvent->message);
  277.         break;
  278.     case updateEvt:            /* These can only happen to our dialogs */
  279.     case activateEvt:
  280.         DialogSelect(gEvent, &dPtr, &itemHit);
  281.     }    /* end gEvent.what */
  282. }
  283.  
  284. /************************************************************************
  285.  *
  286.  *  Function:        main
  287.  *
  288.  *  Purpose:        control this puppy
  289.  *
  290.  *  Returns:        none    
  291.  *
  292.  *  Side Effects:    none inherient in this routine.  Routines called
  293.  *                    from here do all the work.
  294.  *
  295.  *  Description:    Do standard Macintosh initialization.
  296.  *
  297.  ************************************************************************/
  298. void
  299. main(void)
  300. {
  301.     OSErr        result;
  302.     DialogPtr    dPtr;
  303.     
  304.     
  305.     InitGraf((Ptr)&qd.thePort);
  306.     InitFonts();
  307.     FlushEvents(everyEvent, 0);
  308.     InitWindows();
  309.     InitMenus();
  310.     TEInit();
  311.     InitDialogs(0L);
  312.     InitCursor();
  313.     MoreMasters();
  314.     
  315.     SetUpMenus();
  316.  
  317.     gRefNum = 0;
  318.     result = isoOpen(drvName, &gRefNum);
  319.     if (result != noErr)
  320.     {
  321.         ErrorMsg("isoOpen failed with code %d (0x%x)", result, result);
  322.         return;
  323.     }
  324.  
  325.     gQuit = false;
  326.     
  327.     dPtr = GetNewDialog(DU_CenterDLOG(PROMPTID), (DialogPeek)0L, (WindowPtr)-1L);
  328.     do {
  329.         SystemTask();
  330.         if (GetNextEvent(everyEvent, &gEvent)) {
  331.                 HandleEvent(&gEvent);
  332.         }    /* end if */
  333.     } while (gQuit == false);
  334.     DisposDialog(dPtr);
  335.  
  336.     if (gDriveNumber != 0)
  337.     {
  338.         result = Eject(NULL, gDriveNumber);
  339.         if (result != noErr)
  340.             ErrorMsg("can't eject floppy! (%d)", result);
  341.     }
  342. }
  343.  
  344.  
  345. /************************************************************************
  346.  *
  347.  *  Function:        InitializeVolume
  348.  *
  349.  *  Purpose:        initialize volume
  350.  *
  351.  *  Returns:        nothing
  352.  *
  353.  *  Side Effects:    destroys volume and create PVD and VDT instead
  354.  *
  355.  *  Description:
  356.  *
  357.  ************************************************************************/
  358. static void
  359. InitializeVolume(void)
  360. {
  361.     OSErr    result;
  362.     
  363.     result = ZeroDisk(gRefNum);
  364.     if (result == noErr)
  365.     {
  366.         result = CreatePVD(gRefNum);
  367.         if (result == noErr)
  368.             result = CreateVDT(gRefNum);
  369.     }
  370.     if (result != noErr)
  371.         ErrorMsg("Couldn't initialize volume");
  372. }
  373.         
  374.